<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    /**
     * @var Zend_View
     */
    protected $_view;
    
    /**
     * @var Zend_Layout 
     */
    protected $_layout;
    
    /**
     * @var Zend_Controller_Front
     */
    public $frontController;

    /**
     * Set database in registry
     * 
     * Vedere se è utile o no!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
     */
    protected function _initDbReg()
    {
        $db = $this->bootstrap('db');
        Zend_Registry::set('db', $db);
    }

    /**
     * Set config in the registry
     */
    protected function _initConfig()
    {
        Zend_Registry::set('config', $this->getOptions());
        
    }

     /**
     * Setup Zend_Locale
     */
    protected function _initLocale()
    {
        $locale = new Zend_Locale('it_IT');
        Zend_Registry::set('Zend_Locale', $locale);
    }

    /**
     * Setup Zend_View
     */
    protected function _initViewSettings()
    {
        $this->bootstrap('view');
        $this->bootstrap('layout');
        $this->_view = $this->getResource('view');
        
        // Set encoding and doctype
        $this->_view->setEncoding('UTF-8');
        $this->_view->doctype('XHTML1_STRICT');
        
        // set the content type and language
        $this->_view
             ->headMeta()
             ->setHttpEquiv('Content-Type', 'text/html; charset=utf-8') // fa sparire la A-circonflesso
             ->appendHttpEquiv('Content-Language', 'it-IT');            // da fianco al simbolo £ o €
        $this->_view
             ->setEncoding('UTF-8');
        
        // set main css links (Blueprint css framework)
        $this->_view
             ->headLink()
             ->appendStylesheet('/css/main.css');
        // set css links for "Blueprint css Framework"
        $this->_view
             ->headLink()
             ->appendStylesheet('/css/blueprint/ie.css', 'screen, projection', 'IE');
        $this->_view
             ->headLink()
             ->appendStylesheet('/css/blueprint/print.css', 'print');
        $this->_view
             ->headLink()
             ->appendStylesheet('/css/blueprint/screen.css', 'screen, projection');
        $this->_view
             ->headLink()
             ->appendStylesheet('/css/blueprint/plugins/fancy-type/screen.css', 'screen, projection');
        $this->_view
             ->headLink()
             ->appendStylesheet('/css/blueprint/plugins/buttons/screen.css', 'screen, projection');
        $this->_view
             ->headLink()
             ->appendStylesheet('/css/blueprint/plugins/link-icons/screen.css', 'screen, projection');
        
        // set the title
        $this->_view
             ->headTitle('Il Bivacco Toscana - Trekking Escursionismo ');
        
        // set separator string for segments
        $this->_view
             ->headTitle()
             ->setSeparator(' - ');
        
        // Set jQuery-JqueryUI environnement
        $this->_view
             ->addHelperPath("ZendX/JQuery/View/Helper", "ZendX_JQuery_View_Helper");
        $this->_view->jQuery()->addStylesheet($this->_view->baseUrl() . '/js/jquery/css/ui-lightness/jquery-ui-1.8.21.custom.css');
        $this->_view->jQuery()->setLocalPath($this->_view->baseUrl() . '/js/jquery/js/jquery-1.7.2.min.js');
        $this->_view->jQuery()->setUiLocalPath($this->_view->baseUrl() . '/js/jquery/js/jquery-ui-1.8.21.custom.min.js');
        $this->_view->jQuery()->enable();
        $this->_view->jQuery()->uiEnable();
        $viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer();
        $viewRenderer->setView($this->_view);
        Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
        
        // Set personal file for JS scripts 
        $this->_view
             ->headScript()
             ->appendFile('/js/scripts.js', 'text/javascript');
        
        // Setup NivoSlider
        $this->_view
             ->headLink()
             ->appendStylesheet('/js/nivo-slider/nivo-slider.css', 'screen, projection');
        $this->_view
             ->headLink()
             ->appendStylesheet('/js/nivo-slider/themes/default/default.css', 'screen, projection');
        $this->_view
             ->headScript()
             ->appendFile('/js/nivo-slider/jquery.nivo.slider.pack.js', 'text/javascript');
        
        // Setup Lightbox2
        $this->_view
             ->headScript()
             ->appendFile('/js/lightbox/js/lightbox.js', 'text/javascript');
        $this->_view
             ->headLink()
             ->appendStylesheet('/js/lightbox/css/lightbox.css', 'screen, projection');
        
        // Setup megamenu
        $this->_view
             ->headScript()
             ->appendFile('/js/menu/js/jquery.hoverIntent.minified.js', 'text/javascript');
        $this->_view
             ->headScript()
             ->appendFile('/js/menu/js/jquery.dcmegamenu.1.3.3.min.js', 'text/javascript');
        $this->_view
             ->headLink()
             ->appendStylesheet('/js/menu/css/dcmegamenu.css', 'screen, projection');
        $this->_view
             ->headLink()
             ->appendStylesheet('/js/menu/css/skins/orange.css', 'screen, projection');
        
        // Setup easyconfirm
        $this->_view
             ->headScript()
             ->appendFile('/js/jquery.easy-confirm-dialog.js', 'text/javascript');
        
        // Setup Tinymce
         $this->_view
             ->headScript()
             ->appendFile('/js/tiny_mce/tiny_mce.js', 'text/javascript');
    }
    
    /**
     * Setup navigation  
     */
    protected function _initNavigation()
    {
        $this->bootstrap('layout');
        $this->_layout = $this->getResource('layout');
        $this->_view = $this->_layout->getView();
//        $config = new Zend_Config_Xml(APPLICATION_PATH . '/configs/navigation.xml', 'nav');
//        $navigation = new Zend_Navigation($config);
//        $this->_view->navigation($navigation);
        $cats = new Application_Model_Categorys;
        $navArray = $cats->getNavArray();
        $navigation = new Zend_Navigation($navArray);
        $this->_view->navigation($navigation);
    }
    
    /**
     * ATTENZIONE!!! 
     * Le Routes sono una lista LIFO => metto la route più
     * specializzata dopo quella meno specializzata. 
     */
    protected function _initRoutes()
    {
        $this->bootstrap('frontController');
        $router = $this->frontController->getRouter();
        
        
        // Route Home
        $routeHome = new Zend_Controller_Router_Route(
                    '', 
                    array(
                        'action'        => 'index',
                        'controller'    => 'index',
                        'module'        => 'default',
                    )
                );
        $router->addRoute('index_home', $routeHome);
        
        // Route Contact
        $routeContact = new Zend_Controller_Router_Route(
                    '/contact', 
                    array(
                        'action'        => 'index',
                        'controller'    => 'index',
                        'module'        => 'contact',
                    )
                );
        $router->addRoute('index_contact', $routeContact);
        
        
        // Route Eventos
        $routeEventos = new Zend_Controller_Router_Route(
                    'evento/:ident', 
                    array(
                        'action'        => 'show',
                        'controller'    => 'index',
                        'module'        => 'default',
                        'ident'         => '',
                    ), 
                    array(
                        'ident'  => '[a-zA-Z-_0-9]+',
                    )
                );
        $router->addRoute('index_evento', $routeEventos);
        
        // Route Archivio Category Eventos
        $routeArchivioCat = new Zend_Controller_Router_Route(
                    'archivio/:categoryIdent/:page', 
                    array(
                        'action'        => 'archivio',
                        'controller'    => 'index',
                        'module'        => 'default',
                        'categoryIdent' => '',
                        'page'          => 1,
                    ), 
                    array(
                        'ident'     => '[a-zA-Z-_0-9]+',
                        'page'      => '\d+',
                    )
                );
        $router->addRoute('index_archivio_category', $routeArchivioCat);
        
        // Route Archivio Eventos
        $routeArchivio = new Zend_Controller_Router_Route(
                    'archivio/:page', 
                    array(
                        'action'        => 'archivio',
                        'controller'    => 'index',
                        'module'        => 'default',
                        'page'          => 1,
                    ), 
                    array(
                        'page'      => '\d+',
                    )
                );
        $router->addRoute('index_archivio', $routeArchivio);
        
        // Route Proposte
        $routeProposte = new Zend_Controller_Router_Route(
                    'proposte/:page', 
                    array(
                        'action'        => 'index',
                        'controller'    => 'proposta',
                        'module'        => 'default',
                        'page' => 1,
                    ), 
                    array(
                        'page'  => '\d+',
                    )
                );
        $router->addRoute('index_proposta', $routeProposte);
        
        // Route show_proposta
        $routeProposta = new Zend_Controller_Router_Route(
                    'proposta/:propIdent', 
                    array(
                        'action'        => 'show',
                        'controller'    => 'proposta',
                        'module'        => 'default',
                        'propIdent' => '',
                    ), 
                    array(
                        'propIdent'  => '[a-zA-Z-_0-9]+',
                    )
                );
        $router->addRoute('show_proposta', $routeProposta);
        
        // Route showbycat_proposta
        $routePropostas = new Zend_Controller_Router_Route(
                    'proposta/cat/:catIdent', 
                    array(
                        'action'        => 'showbycat',
                        'controller'    => 'proposta',
                        'module'        => 'default',
                        'catIdent' => '',
                    ), 
                    array(
                        'catIdent'  => '[a-zA-Z-_0-9]+',
                    )
                );
        $router->addRoute('showbycat_proposta', $routePropostas);
        
        // Route Guidas
        $routeGuidas = new Zend_Controller_Router_Route(
                    'guida/:ident', 
                    array(
                        'action'        => 'index',
                        'controller'    => 'guida',
                        'module'        => 'default',
                        'ident' => '',
                    ), 
                    array(
                        'ident'  => '[a-zA-Z-_0-9]+',
                    )
                );
        $router->addRoute('index_guida', $routeGuidas);
        
        // Route Invia iscrizione
        $routeIscrizione = new Zend_Controller_Router_Route(
                    'iscrizione/:ident', 
                    array(
                        'action'        => 'index',
                        'controller'    => 'iscrizione',
                        'module'        => 'default',
                        'ident'         => '',
                    ), 
                    array(
                        'ident'  => '[a-zA-Z-_0-9]+',
                    )
                );
        $router->addRoute('index_iscrizione', $routeIscrizione);
        
        // Route lista iscrizioni
        $routeListaIscrizioniAttive = new Zend_Controller_Router_Route(
                    'lista-iscrizioni', 
                    array(
                        'action'        => 'list-actives',
                        'controller'    => 'iscrizione',
                        'module'        => 'default',
                    )
                );
        $router->addRoute('lista-iscrizioni-attive', $routeListaIscrizioniAttive);
        
        // Archivio lista iscrizioni
        $routeArchivioIscrizioni = new Zend_Controller_Router_Route(
                    'archivio-iscrizioni/:id', 
                    array(
                        'action'        => 'list',
                        'controller'    => 'iscrizione',
                        'module'        => 'default',
                        'id'            => 0,
                    ),
                    array(
                            'id'  => '\d+',
                    )
                );
        $router->addRoute('archivio-iscrizioni', $routeArchivioIscrizioni);
    }
    
    public function _initSearchLucene()
    {
        Zend_Search_Lucene_Search_QueryParser::setDefaultEncoding('utf-8');
        Zend_Search_Lucene_Analysis_Analyzer::setDefault(
            new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8_CaseInsensitive ()
        );

    }
    
    public function _initSession()
    {
        $configSession = new Zend_Config_Ini(APPLICATION_PATH . '/configs/session.ini', APPLICATION_ENV);
        Zend_Session::setOptions($configSession->toArray());
    }
    
}
